<?php


namespace app\common\model;


use app\common\enum\MainEnum;
use app\common\service\Redis;

class ProductModel extends CommonModel
{
    protected $name = 'product';

    public function attrInfo(): \think\model\relation\HasMany
    {
        return $this->hasMany(ProductAttrModel::class, 'product_id', 'id');
    }

    public function skuInfo(): \think\model\relation\HasMany
    {
        return $this->hasMany(ProductSkuModel::class, 'product_id', 'id');
    }

    public function categoryInfo(): \think\model\relation\HasOne
    {
        return $this->hasOne(ProductCategoryModel::class, 'id', 'category_id');
    }

    public function getImageUrlsAttr($v)
    {
        return explode('|', $v);
    }

    public function getExtendPricePackageAttr()
    {
        return Redis::getStoreConfig('packagePrice');
    }

    public function setImageUrlsAttr($v)
    {
        return implode('|', $v);
    }

    public function getAttrInfoAttr($v)
    {
        return json_decode($v, true);
    }

    public function setAttrInfoAttr($v)
    {
        return json_encode($v, JSON_UNESCAPED_UNICODE);
    }

    public function getExtendStatusTextAttr($v, $data): ?string
    {
        foreach (MainEnum::PRODUCT_STATUS as $key => $item) {
            if ($data['status'] === $item) {
                return $key;
            }
        }
        return '';
    }

    public function setTagsAttr($v): ?string
    {
        return implode('|', $v);
    }

    public function getTagsAttr($v)
    {
        return explode('|', $v);
    }

    /**
     * 获取商品信息，id或ids
     */
    public function getProduct($where = [])
    {
        return $this->with([
            'attrInfo',
            'skuInfo',
            'categoryInfo',
        ])->order([
            //            'sea_waybill_number' => 'desc',
            //            'case_number'        => 'desc',
            'create_time' => 'desc',
        ])->where($where)->append([
            'extend_desc',
            'extend_type_text',
            'extend_status_text',
            'extend_price_package',
        ])->find();
    }

    public function getProductList($where = [])
    {
        return $this->with([
            'attrInfo',
            'skuInfo',
            'categoryInfo',
        ])->order([
            'create_time' => 'desc',
        ])->where($where)->append([
            'extend_desc',
            'extend_type_text',
            'extend_status_text',
            'extend_price_package',
        ])->select();
    }

    public function getProductListPaginate($where = [], $page = 1, $limit = 10)
    {
        return $this->with([
            'attrInfo',
            'skuInfo',
            'categoryInfo',
        ])->order([
            'create_time' => 'desc',
        ])->where($where)->append([
            'extend_desc',
            'extend_type_text',
            'extend_status_text',
            'extend_price_package',
        ])->paginate([
            'page'      => $page,
            'list_rows' => $limit,
        ]);
    }
}